//
//  AFLaunchManager.m
//  AFChat
//
//  Created by Void on 2018/6/19.
//  Copyright © 2018年 afei. All rights reserved.
//

#import "AFLaunchManager.h"
#import "AFUserHelper.h"

#import "AFAccountViewController.h"
#import "AFConversationViewController.h"
#import "AFContactsViewController.h"
#import "AFDiscoverViewController.h"
#import "AFMineViewController.h"


@interface AFLaunchManager ()

@property (nonatomic, weak) UIWindow *window;

@end

@implementation AFLaunchManager
@synthesize tabBarController = _tabBarController;

+ (AFLaunchManager *)sharedInstance {
    static AFLaunchManager *rootVCManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        rootVCManager = [[self alloc] init];
    });
    return rootVCManager;
}

- (void)launchInWindow:(UIWindow *)window {
    self.window = window;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    if ([AFUserHelper sharedHelper].isLogin) { //已登陆
        [self.tabBarController setViewControllers: [self createTabberChildViewController]];
        [self setCurRootVC:self.tabBarController];
    }else{
        AFAccountViewController *accountVC = [[AFAccountViewController alloc] init];
        TLWeakSelf(self);
        [accountVC setLoginSuccess:^{
            TLStrongSelf(weakself);
            [strongweakself launchInWindow:window];
        }];
        [self setCurRootVC:accountVC];
    }
}

#pragma mark - # Private Methods
- (NSArray *)createTabberChildViewController {
    
    AFConversationViewController *conversationVC = [[AFConversationViewController alloc] init];
    AFContactsViewController *contaceVC = [[AFContactsViewController alloc] init];
    AFDiscoverViewController *discoverVC = [[AFDiscoverViewController alloc] init];
    AFMineViewController *mineVC = [[AFMineViewController alloc] init];
    
    NSArray *date = @[addNavigationController(conversationVC),
                      addNavigationController(contaceVC),
                      addNavigationController(discoverVC),
                      addNavigationController(mineVC)];
    return date;
}


#pragma mark - Gerter/Setter
- (void)setCurRootVC:(__kindof UIViewController *)curRootVC {

    _curRootVC = curRootVC;
    
    UIWindow *window = self.window ? self.window : [UIApplication sharedApplication].keyWindow;
    [window removeAllSubviews];
    [window setRootViewController:curRootVC];
    [window addSubview:curRootVC.view];
    [window makeKeyAndVisible];
}

- (TLTabBarController *)tabBarController {
    
    if (!_tabBarController) {
        TLTabBarController *tabbarController = [[TLTabBarController alloc] init];
        [tabbarController.tabBar setBackgroundColor:[UIColor colorGrayBG]];
        [tabbarController.tabBar setTintColor:[UIColor colorGreenDefault]];
        _tabBarController = tabbarController;
    }
    
    return _tabBarController;
}

@end
